<template>
  <el-dialog @close="close" v-model="dialogVisible" :title="title" width="50%">
    <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="100px">
      <el-form-item label="医院平台" prop="platform_id" v-if="!route.query.id">
        <el-select
          v-model="ruleForm.platform_id"
          clearable
          filterable
          placeholder="请选择"
          style="width: 100%"
        >
          <el-option v-for="item in platList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="上级部门" prop="parentid">
        <el-select
          v-model="ruleForm.parentid"
          clearable
          filterable
          placeholder="请选择"
          style="width: 100%"
        >
          <el-option v-for="item in deptList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="科室名称" prop="name">
        <el-input v-model="ruleForm.name" placeholder="请输入科室名称" />
      </el-form-item>
      <el-form-item label="科室介绍" prop="introduction">
        <el-input v-model="ruleForm.introduction" type="textarea" placeholder="请输入科室介绍" />
      </el-form-item>
      <el-form-item label="联系人" prop="contact">
        <el-input v-model="ruleForm.contact" placeholder="请输入联系人" />
      </el-form-item>
      <el-form-item label="联系电话" prop="mobile">
        <el-input v-model="ruleForm.mobile" placeholder="请输入联系电话" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-switch
          v-model="ruleForm.status"
          inline-prompt
          active-text="启用"
          inactive-text="禁用"
          :active-value="1"
          :inactive-value="0"
        ></el-switch>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" :loading="saveLoading" @click="handleConfim"> 确定 </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script lang="ts" setup>
  import { ElMessageBox, ElMessage, FormInstance, ElNotification } from 'element-plus'
  import { reactive, ref } from 'vue'
  import { departmentList, addDept, editDept, platformList } from '@/api/admin'
  import { useRoute } from 'vue-router'
  const route = useRoute()
  const ruleFormRef = ref<FormInstance>()
  const dialogVisible = ref<boolean>(false)
  const title = ref('科室')

  const rules = reactive({
    platform_id: [{ required: true, message: '请选择医院平台', trigger: 'change' }],
    name: [{ required: true, message: '请输入科室名称', trigger: 'blur' }],
    contact: [{ required: true, message: '请输入联系人', trigger: 'blur' }],
    mobile: [{ required: true, message: '请输入联系电话', trigger: 'blur' }],
  })
  const ruleForm = reactive({
    platform_id: route.query.id || '',
    id: '',
    name: '',
    introduction: '',
    parentid: '',
    contact: '',
    mobile: '',
    status: 1,
  })
  const deptList = ref() //上级部门
  const platList = ref() //医院平台
  const saveLoading = ref(false)
  const emit = defineEmits<{ (event: 'refresh'): void }>()

  function close() {
    ruleFormRef.value.resetFields()
    Object.keys(ruleForm).forEach((key) => {
      if (key == 'status') ruleForm[key] = 1
      else if (key == 'platform_id') ruleForm[key] = route.query.id
      else if (key == 'parentid') ruleForm[key] = ''
      else ruleForm[key] = ''
    })
  }

  const show = (item = {}) => {
    title.value = '新增科室'
    if (item.id) {
      title.value = '编辑科室'
      Object.keys(ruleForm).forEach((key) => {
        if (key == 'parentid' && ruleForm[key] == 0) ruleForm[key] = ''
        else if (key == 'platform_id') ruleForm[key] = Number(item[key])
        else ruleForm[key] = item[key]
      })
    }
    dialogVisible.value = true
    getDeptList()
    if (!route.query.id) getPlatList()
  }

  //医院平台列表
  const getPlatList = () => {
    platformList({ selectFields: 'id,name' }).then((res) => {
      if (res.code == 200) {
        platList.value = res.data
        if (res.data.length == 1) route.query.id = ruleForm.platform_id = res.data[0].id
      }
    })
  }

  //上级部门列表
  const getDeptList = () => {
    let filter = JSON.stringify({ parentid: 0 })
    departmentList({
      platform_id: route.query.id,
      selectFields: 'id,name',
      filter: filter,
    }).then((res) => {
      if (res.code == 200) {
        deptList.value = res.data
      }
    })
  }

  const handleConfim = async (done: () => void) => {
    await ruleFormRef.value.validate((valid, fields) => {
      if (valid) {
        saveLoading.value = true
        if (ruleForm.id) {
          //编辑
          editDept(ruleForm).then((res) => {
            if (res.code == 200) {
              ElNotification({
                title: '编辑成功',
                message: res.msg,
                type: 'success',
              })
              emit('refresh')
            }
            dialogVisible.value = false
            saveLoading.value = false
          })
        } else {
          //新增
          addDept(ruleForm).then((res) => {
            if (res.code == 200) {
              ElNotification({
                title: '添加成功',
                message: res.msg,
                type: 'success',
              })
              emit('refresh')
            }
            dialogVisible.value = false
            saveLoading.value = false
          })
        }
      }
    })
  }

  defineExpose({
    show,
  })
</script>
<style lang="scss" scoped></style>
